package org.example.flea_market_mvc.service.categories.impl;

import org.example.flea_market_mvc.entity.Categories;
import org.example.flea_market_mvc.mapper.CategoriesMapper;
import org.example.flea_market_mvc.param.CategoryParam;
import org.example.flea_market_mvc.service.categories.CategoriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分类服务实现类
 * 处理分类创建、更新、查询等业务逻辑，支持树形分类结构
 */
@Service
public class CategoriesServiceImpl implements CategoriesService {

    @Autowired
    private CategoriesMapper categoriesMapper;

    @Override
    public int insert(CategoryParam categoryParam) {
        Categories category = new Categories();
        BeanUtils.copyProperties(categoryParam, category);
        return categoriesMapper.insert(category);
    }

    @Override
    public int deleteById(Integer id) {
        return categoriesMapper.deleteById(id);
    }

    @Override
    public int update(CategoryParam categoryParam) {
        Categories category = new Categories();
        BeanUtils.copyProperties(categoryParam, category);
        return categoriesMapper.update(category);
    }

    @Override
    public Categories selectById(Integer id) {
        return categoriesMapper.selectById(id);
    }

    @Override
    public List<Categories> selectAll() {
        return categoriesMapper.selectAll();
    }

    @Override
    public org.example.flea_market_mvc.entity.PageInfo<Categories> selectPage(CategoryParam param) {
        int pageNum = param.getPageNum() == null ? 1 : param.getPageNum();
        int pageSize = param.getPageSize() == null ? 10 : param.getPageSize();
        int offset = (pageNum - 1) * pageSize;
        List<Categories> list = categoriesMapper.selectPage(param, offset, pageSize);
        int total = categoriesMapper.countByParam(param);
        return new org.example.flea_market_mvc.entity.PageInfo<>(pageNum, pageSize, total, list);
    }
}